package org.daymr.puzzle;

import org.daymr.puzzle.view.GameView;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

/**入口
 * @author Ben
 * @date 2014年12月19日 下午11:01:56
 */
public class MainActivity extends Activity {
	
	/**游戏视图*/
	private GameView mGameView;
	
	/**渲染开关*/
	public boolean isRenderer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//去标题
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		if(supportES2()){//若支持OpenGL ES 2.0
			isRenderer = true;
			mGameView = new GameView(this);
			setContentView(mGameView);
		}else{//若不支持
			Toast.makeText(this, "设备不支持OpenGL ES 2.0 .", Toast.LENGTH_SHORT).show();
			setContentView(R.layout.activity_main);
		}
		
	}


	/**
	 * 判断是否支持OpenGL ES 2.0
	 * @return true则说明设备支持OpenGL ES 2.0，否则不支持
	 */
	private boolean supportES2(){
		ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
		ConfigurationInfo cfi = am.getDeviceConfigurationInfo();
		return cfi.reqGlEsVersion>=0x20000
				|| (Build.VERSION.SDK_INT>=Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1
				&&(Build.FINGERPRINT.startsWith("generic")
						|| Build.FINGERPRINT.startsWith("unknown")
						|| Build.MODEL.contains("google_sdk")
						|| Build.MODEL.contains("Emulator")
						|| Build.MODEL.contains("Android SDK build for x86")));
	}

	
	@Override
	protected void onResume() {
		super.onResume();
		if(isRenderer)
			mGameView.onResume();
	}
	
	
	@Override
	protected void onPause() {
		super.onPause();
		if(isRenderer)
			mGameView.onPause();
	}
	
}
